// pages/bankCard/addBankCard.js
const app = getApp()
import Api from '../../utils/api';
import util from '../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    realName: '', //真实姓名
    phonenumber: '', //商户手机号
    cardNo: '', //身份证号
    bankCardNo: '', //银行卡号
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    app.globalData.otherPageHideFlag = true;
  },

  //姓名
  getRealName(e){
    let _this = this;
    _this.setData({
      realName: e.detail.value,
    })
  },
  //银行卡号
  getBankCardNo(e){
    let _this = this;
    _this.setData({
      bankCardNo: e.detail.value,
    })
  },

  //身份证号
  getCardNo(e) {
    let _this = this;
    _this.setData({
      cardNo: e.detail.value,
    })
  },
  //手机号码
  getPhoneNumber(e){
    let _this = this;
    _this.setData({
      phonenumber: e.detail.value,
    })
  },

  //提交
  submintBtn(){
    let _this = this;
    if (_this.data.realName == '' || _this.data.bankCardNo == '' || _this.data.cardNo == '' || _this.data.phonenumber == ''){
      wx.showToast({
        title: '信息不全',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    let myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (!myreg.test(_this.data.phonenumber)) {
      wx.showToast({
        title: '手机号码不正确',
        icon: 'none',
        duration: 2000
      })
      return;
    }

    let iscard = /^\d{17}([0-9]|X)$/;
    if (!iscard.test(_this.data.cardNo)) {
      wx.showToast({
        title: '身份证不正确',
        icon: 'none',
        duration: 2000
      })
      return;
    }

    let num = /^\d*$/;
    let strBin = "10,18,30,35,37,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,58,60,62,65,68,69,84,87,88,94,95,98,99";
    if (_this.data.bankCardNo.length < 16 || _this.data.bankCardNo.length > 19 || !num.exec(_this.data.bankCardNo) || strBin.indexOf(_this.data.bankCardNo.substring(0, 2)) == -1) {
      wx.showToast({
        title: '银行卡不正确',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    let data = {
      "phonenumber": _this.data.phonenumber,
      "realName": _this.data.realName,
      "cardNo": _this.data.cardNo,
      "bankCardNo": _this.data.bankCardNo
    }
    wx.showLoading({
      title: '加载中',
    })
    Api.bindBankCard(data).then( res => {
      //console.log(res)
      wx.hideLoading();
      wx.showToast({
        title: '绑定成功',
        icon: 'success',
        duration: 2000
      })
      setTimeout(function () {
        wx.navigateBack({
          delta: 1,
        });
      }, 2000)
    })

  }




})